package com.kongmou.springReview.commonCfg.el;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

/**
 * 測試EL 表达式
 * 
 * @author kongmou
 * @date 2018年3月5日下午5:52:09
 * @since jdk 1.8
 * 
 * @version 1.0
 */
@Configuration
@ComponentScan
@PropertySource({ "classpath:test.properties" })
// @PropertySource("classpath:test.properties")
public class ElConfig {

	// 注入普通字符串
	@Value("普通字符串")
	private String normal;

	// 注入操作系统属性
	@Value("#{systemProperties['os.name']}")
	private String osName;

	// 注入表达式结果
	@Value("#{ T(java.lang.Math).random() * 100.0}")
	private double randomNumber;

	// 主其他Bean属性
	@Value("#{demoElService.beanProperties}")
	private String fromAnother;

	// 注入文件资源
	@Value("classpath:test.txt")
	private Resource testFile;

	// 注入网址资源
	@Value("http://www.baidu.com")
	private Resource testUrl;

	// 注入配置文件
	@Value("book.name")
	private String bookName;

	@Autowired
	private Environment environment;

	@Bean
	public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

	public void outPut() {
		try {
			System.out.println(normal);
			System.out.println(osName);
			System.out.println(randomNumber);
			System.out.println(fromAnother);
			System.out.println(IOUtils.toString(testFile.getInputStream(), "utf-8"));
			System.out.println(IOUtils.toString(testUrl.getInputStream(), "utf-8"));
			System.out.println(bookName);
			System.out.println(environment.getProperty("book.reader"));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
